/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.usecase.command;

import org.eclipse.core.runtime.Assert;
import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Include;
import org.eclipse.uml2.uml.UseCase;

import com.cea.papyrus.core.utils.Debug;

/**
 * Manages removal of includes.
 * 
 * @author Gilles Cannenterre
 * @since 30 nov. 06
 */
public class DeleteIncludeFromModelCommand extends Command {
	
	/**
	 * debug channel.
	 */
	private int channel = 201;
	
	/**
	 * Include to delete.
	 */
	private Include child;
	
	/**
	 * Owner of the include to delete.
	 */
	private UseCase parent;

	/**
	 * 
	 */
	private UseCase addition;

	/**
	 * 
	 */
	private UseCase includingUseCase;
	
	/**
	 * Default Constructor.
	 * 
	 * @param child the child
	 */
	public DeleteIncludeFromModelCommand(Include child) {
		if (child == null) {
			throw new IllegalArgumentException();
		}
		setLabel("Delete <Include>");
		this.child = child;
		addition = child.getAddition();
		includingUseCase = child.getIncludingCase();
		this.parent = child.getIncludingCase();
	}
	
	/**
	 * Execute.
	 */
	@Override
	public void execute() {
		Debug.debug(this, "DeleteIncludeFromModel.execute -> "+child, channel);
		Assert.isNotNull(parent, "DeleteInclude.child has no parent");
		if (parent instanceof Classifier) {
			child.eAdapters().clear();	
			child.setAddition(null);
			child.setIncludingCase(null);
			parent.getIncludes().remove(child);
		} else {
			Debug.error(this, "Include.delete is not in a classifier", channel);
		}
	}

	/**
	 * Redo.
	 */
	@Override
	public void redo() {
		execute();
	}

	/**
	 * Undo.
	 */
	@Override
	public void undo() {
		if (parent instanceof Classifier) {
			child.setAddition(addition);
			child.setIncludingCase(includingUseCase);
			parent.getIncludes().add(child);
		}
	}
}